﻿Imports System.Data.Entity

<Authorize>
Public Class LocationController
    Inherits System.Web.Mvc.Controller

    Public db As New LocationDBContext

    '
    ' GET: /Location/

    Function Index() As ActionResult
        Return View(db.Locations.ToList())
    End Function

    '
    ' GET: /Location/Details/5

    Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim location As Location = db.Locations.Find(id)
        If IsNothing(location) Then
            Return HttpNotFound()
        End If
        Return View(location)
    End Function

    '
    ' GET: /Location/Create

    Function Create() As ActionResult

        ' Create a list of Companies from table dbo.Companies. 
        Dim co As New List(Of SelectListItem)()
        Dim companies As New List(Of Company)()


        Dim cc As MVC1.CompanyController = New CompanyController()
        companies = cc.db.Companies.ToList()

        For Each item As Company In companies
            co.Add(New SelectListItem() With {.Text = item.Name, .Value = item.ID})
        Next

        ViewBag.ddCompanies = co


        Return View()
    End Function

    '
    ' POST: /Location/Create

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Create(ByVal location As Location) As ActionResult
        If ModelState.IsValid Then

            Dim CompanyID As String = Request("ddCompanies")

            location.CompanyID = CompanyID

            db.Locations.Add(location)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(location)
    End Function

    '
    ' GET: /Location/Edit/5

    Function Edit(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim location As Location = db.Locations.Find(id)
        If IsNothing(location) Then
            Return HttpNotFound()
        End If
        Return View(location)
    End Function

    '
    ' POST: /Location/Edit/5

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Edit(ByVal location As Location) As ActionResult
        If ModelState.IsValid Then
            db.Entry(location).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(location)
    End Function

    '
    ' GET: /Location/Delete/5

    Function Delete(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim location As Location = db.Locations.Find(id)
        If IsNothing(location) Then
            Return HttpNotFound()
        End If
        Return View(location)
    End Function

    '
    ' POST: /Location/Delete/5

    <HttpPost()> _
    <ActionName("Delete")> _
    <ValidateAntiForgeryToken()> _
    Function DeleteConfirmed(ByVal id As Integer) As RedirectToRouteResult
        Dim location As Location = db.Locations.Find(id)
        db.Locations.Remove(location)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        db.Dispose()
        MyBase.Dispose(disposing)
    End Sub

End Class